package egg_drop;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


public class ObBasket {
	
	private Random random;
	
	public int x;
	
	public int y;
	
	public int travelSpeed;
	
	public int travelAccel;
	
	public BufferedImage basketImg;
	
	public int imgWidth;
	
	public int imgHeight;
	
	public ObBasket()
	{
		Initialize();
		LoadContent();
		
		x = random.nextInt(Framework.frameWidth - imgHeight);
	}
	
	private void Initialize()
    {
        random = new Random();      
        ResetPlayer();                
        travelAccel = 6;
    }
	
	private void LoadContent()
	{
		try {		
		
		URL baskImgUrl = this.getClass().getResource("/egg_drop/resources/basket.png");
		basketImg = ImageIO.read(baskImgUrl);
		imgWidth = basketImg.getWidth();
		imgHeight = basketImg.getHeight();
		}
		catch(IOException ex) {
            Logger.getLogger(ObBasket.class.getName()).log(Level.SEVERE, null, ex);
        }
	}
	
	public void ResetPlayer()
	{
		x = random.nextInt(Framework.frameWidth - imgHeight);
		y = 50;
		travelSpeed = 0;
	}
	
	public void Update(Point mousePosition)
	{
		if(Canvas.keyboardKeyState(KeyEvent.VK_LEFT) || Canvas.keyboardKeyState(KeyEvent.VK_NUMPAD4))
			travelSpeed = 0 - travelAccel;
		else if (Canvas.keyboardKeyState(KeyEvent.VK_RIGHT) || Canvas.keyboardKeyState(KeyEvent.VK_NUMPAD6))
			travelSpeed = 0 + travelAccel;
		else travelSpeed = 0;
		
		if(Canvas.mouseButtonState(MouseEvent.BUTTON1))
		{
			if (mousePosition.x < x)
				travelSpeed = 0 - travelAccel;
			else if (mousePosition.x > x)
			{
				travelSpeed = 0 + travelAccel;
			}
			else travelSpeed = 0;
		}
		
		if((x >= 0) && (x + imgWidth <= Framework.frameWidth))
		{
			x += travelSpeed;
		}
		else if (x < 0){
			x = Framework.frameWidth - imgWidth;
		}
		else 
		{
			x = 0;
		}
				
		y = Framework.frameHeight - 50 - imgHeight;
			
	}	
	
	public void Draw(Graphics2D g2d)
	{
		g2d.drawImage(basketImg, x, y , null);
		//If animation
	}
		

}
